package com.zbkj.front.controller.pay;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zbkj.common.model.user.UserToken;
import com.zbkj.common.response.CommonResult;
import com.zbkj.common.vo.WeChatMiniAuthorizeVo;
import com.zbkj.service.service.user.UserTokenService;
import com.zbkj.service.service.wechat.WechatNewService;
import com.zbkj.service.thirdparty.login.enums.LoginTypeEnum;
import com.zbkj.service.thirdparty.login.request.WeixinXcxLoginRequest;
import com.zbkj.service.thirdparty.login.response.LoginResponse;
import com.zbkj.service.thirdparty.pay.weixin.PayYwTypeHandleService;
import com.zbkj.service.thirdparty.pay.weixin.WeixinPayXcxService;
import com.zbkj.service.thirdparty.pay.weixin.enums.PayYwTypeEnum;
import com.zbkj.service.thirdparty.pay.weixin.request.WeixinPayXcxV3Request;
import com.zbkj.service.thirdparty.pay.weixin.response.PayXcxResponse;
import com.zbkj.service.thirdparty.pay.weixin.vo.PayYwTypeRechargeIntegralVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("api/front/pay/wxxcx")
@Api(tags = "支付-微信小程序支付")
public class PayWxXcxController {

    @Autowired
    private WeixinPayXcxService weixinPayXcxService;

    @Autowired
    private PayYwTypeHandleService payYwTypeService;

    @Autowired
    private WechatNewService wechatNewService;

    @Autowired
    private UserTokenService userTokenService;

    @ApiOperation(value = "绑定微信小程序openid")
    @RequestMapping(value = "/bindWeixinXcxOpenid", method = RequestMethod.GET)
    @ApiImplicitParam(name = "code",value = "微信小程序用户code-临时授权code",required = true)
    public CommonResult<Boolean> bindWeixinXcxOpenid(String code){
        //小程序登录凭证校验
        WeChatMiniAuthorizeVo response = wechatNewService.miniAuthCode(code);
        log.info("小程序获取登录凭证成功 = {}",JSON.toJSONString(response));

        //检测是userToken否存在
        UserToken userToken = userTokenService.getByOpenidAndType(response.getOpenId(), LoginTypeEnum.WX_PROGRAM.getLoginTokenType());
        if (!ObjectUtil.isNotNull(userToken)) {
            //存openid
            userTokenService.bind(
                    response.getOpenId(),
                    LoginTypeEnum.WX_PROGRAM.getLoginTokenType(),
                    userToken.getUid());
        }
        return CommonResult.success(Boolean.TRUE);
    }

    @ApiOperation(value = "微信支付-微信小程序支付（V3）- 支付之前的验证")
    @RequestMapping(value = "/validateOpenid", method = RequestMethod.GET)
    public CommonResult<Boolean> validateOpenid() {
        return CommonResult.success(weixinPayXcxService.validateOpenid());
    }

    @ApiOperation(value = "微信支付-微信小程序支付（V3）- 下单参数")
    @RequestMapping(value = "/weixinXcxPayV3", method = RequestMethod.POST)
    public CommonResult<PayXcxResponse> weixinXcxPayV3(@RequestBody @Validated WeixinPayXcxV3Request payParam) {
        return CommonResult.success(payYwTypeService.wxXcxPayYw(payParam));
    }

    @ApiOperation(value = "微信支付-微信小程序支付（V3）- 回调接口")
    @RequestMapping(value = "/wxXcxPayCallback", method = RequestMethod.POST)
    public Map<String,String> weixinXcxPayV3Callback(HttpServletRequest request) {
        return weixinPayXcxService.callback(request);
    }

    @ApiOperation(value = "微信支付-微信小程序支付（V3）- 查询支付结果")
    @RequestMapping(value = "/selectPayResult", method = RequestMethod.GET)
    @ApiImplicitParam(name = "outTradeNo",value = "订单号",required = true)
    public CommonResult<Boolean> selectPayResult(@RequestParam String outTradeNo) {
        return CommonResult.success(weixinPayXcxService.selectPayResult(outTradeNo));
    }

}
